//
//  StoreAddress.m
//  AutoHome
//
//  Created by wqh on 15-5-14.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "StoreAddress.h"

@interface StoreAddress ()<MKMapViewDelegate>
{
    CGFloat _longitude;
    CGFloat _latitude;
}
//地址解析器
@property (nonatomic) CLGeocoder *geocder;

@end

@implementation StoreAddress

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //创建地址解析器
    self.geocder = [[CLGeocoder alloc] init];
    //解析字符串地址
    [self encodeAddress:self.address];
    //根据解析到的经纬度 创建地图
    
    
    
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBar.translucent = YES;
     [[NSNotificationCenter defaultCenter] postNotificationName:@"displayButton" object:@(0)];
}

//解析字符串地址获得经纬度
- (void)encodeAddress:(NSString *)address {
    if (address != nil && address.length>0) {
        [self.geocder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
            //如果解析结果的集合元素的个数大于1 则表明解析得到了经纬度信息
            if (placemarks.count > 0) {
                //只处理第一个解析结果，实际项目中可使用列表让用户选择
                CLPlacemark *placemark = placemarks[0];
                CLLocation *location = placemark.location;
                _longitude = location.coordinate.longitude;
                _latitude = location.coordinate.latitude;
                if (_longitude!=0 && _latitude!=0) {
                    [self displayMapView];
                }
                
            }
        }];
    }
}

#pragma mark - 通过解析出来的经纬度 展示地图
- (void)displayMapView {
    self.mapView.mapType = MKMapTypeStandard;
    self.mapView.zoomEnabled = YES;
    self.mapView.scrollEnabled = YES;
    self.mapView.rotateEnabled = YES;
    self.mapView.showsUserLocation = YES;
    self.mapView.delegate = self;
    [self locateToLatitude:_latitude longitude:_longitude];
}

- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude {
    CLLocationCoordinate2D center = {latitude,longitude};
    MKCoordinateSpan span ;
    span.latitudeDelta = 0.1;
    span.longitudeDelta = 0.1;
    MKCoordinateRegion region = {center,span};
    [self.mapView setRegion:region animated:YES];
    
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = center;
    point.title = self.titleName;
    point.subtitle = self.minPrice;
    [self.mapView addAnnotation:point];
    [self.mapView selectAnnotation:point animated:YES];
}

#pragma mark MKMapViewDelegate

//自定义锚点控件
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    
    static NSString *annoid = @"AnnoId";
    MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoid];
    if (!annoView) {
        annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoid];
    }
    annoView.image = [UIImage imageNamed:@"pinRed"];
    annoView.canShowCallout = YES;
    return annoView;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
